package com.hngy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hngy.common.R;
import com.hngy.domain.Category;
import com.hngy.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiKe
 * @date 2021/12/26 11:54
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category){
        log.info("执行新增分类：{}",category);
        categoryService.save(category);
        return R.success("新增");

    }

    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize){
        log.info("page = {},pageSize = {}",page,pageSize);
        IPage<Category> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(Long id){
        log.info("删除分类，id为：{}",id);

        categoryService.deleteById(id);

        return R.success("删除成功");
    }

    /**
     * 根据id修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    @GetMapping("/list")
    public R listType(String type){
        log.info("type = {}",type);

        List<Category> list = categoryService.listType(type);

        return R.success(list);
    }


}
